﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.DAL;
using WXYW.BLL;


namespace WXYW
{
    public partial class OriginalApplicationDetailBJXX : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["RType"]) &&
                    Request.QueryString["RType"].Equals("1"))
                {
                    returnHyperLink.NavigateUrl = string.Format("ApplicationDetailBJXX.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID,
                        Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]);
                }
                BindApplication();
            }
        }

        private void BindApplication()
        {
            ApplicationService service = new ApplicationService();
            ApplicationFormHistory application = service.GetOriginalApplicationDetailByID(long.Parse(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
            //RMACodeLabel.Text = application.ApplicationCode;
            if (application != null)
            {
                statusLabel.Text = application.Status;
                runningNumberLabel.Text = application.RunningNumber;
                companyLabel.Text = application.User.CompanyName;

                addressLabel.Text = application.User.UserAddress;
                applyTimeLabel.Text = application.ApplicationDateTime == null ? "" : Convert.ToDateTime(application.ApplicationDateTime).ToString("yyyy-MM-dd");

                faxLabel.Text = application.User.UserFax;
                contactLabel.Text = application.User.ContactPerson;
                emailLabel.Text = application.User.UserEmail;
                cellPhoneLabel.Text = application.User.UserMobilePhone;
                agentLabel.Text = application.ProviderName;
                runningNumberLabel.Text = application.RunningNumber;
               
                ApplicationForm currentApplication = service.GetApplicationByRunningNO(application.RunningNumber);
                if (currentApplication != null)
                {
                    if (!currentApplication.ApplicationProduct.IsLoaded)
                    {
                        currentApplication.ApplicationProduct.Load();
                    }
                    productGridView.DataSource = currentApplication.ApplicationProduct;
                    shxxLbl.Text = currentApplication.BJXXSHAddress;
                }
                else
                {
                    productGridView.DataSource = application.ApplicationProductHistory;
                }
                phoneNumberLabel.Text = application.User.UserPhone;
                productGridView.DataBind();
            }
        }

        protected void productGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();


                Label applyTypeLbl = e.Row.FindControl("applyTypeLbl") as Label;
                ApplicationProduct product = e.Row.DataItem as ApplicationProduct;
                if (product.ApplicationForm.ApplicationType.Equals("BB"))
                {
                    applyTypeLbl.Text = "备件先行";
                }
                else if (product.ApplicationForm.ApplicationType.Equals("BD"))
                {
                    applyTypeLbl.Text = "DOA";
                }

                CheckBox ybChkBox = e.Row.FindControl("ybChkBox") as CheckBox;
                if (product.BJSQYBService.HasValue && product.BJSQYBService.Value)
                {
                    ybChkBox.Checked = true;
                }
            }
        }

        protected void viewLinkButton_click(object sender, EventArgs e)
        {
            string applicationid = ((LinkButton)sender).CommandArgument;
            Response.Redirect(string.Format("ApplicationDetail.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, applicationid));
        }
    }
}